<?php 
header('Content-type:text/html;charset=utf-8');
  $input = file_get_contents("php://input"); //½ÓÊÕPOSTÊý¾Ý

 
    echo "----------input--------<br />";
    var_dump(file_get_contents('php://input', 'r'));
    echo "----------post---------<br />";
    var_dump($_POST);

	file_put_contents('txt/sbsjbh.txt',$input,FILE_APPEND);

     echo "string";
    if($input){
	    $arr = json_decode($input,true);
	    if(array_key_exists('service', $arr)&&array_key_exists('data', $arr['service'])&&array_key_exists('Alarm_check', $arr['service']['data'])&&array_key_exists('Alarm_fire', $arr['service']['data'])){
	    // if(array_key_exists('notifyType',$arr) && ($arr['notifyType']=='deviceDataChanged')){
			if(($arr['service']['data']['Alarm_check']==1)||($arr['service']['data']['Alarm_fire']==1)){
				$eventTime = $arr['service']['eventTime'];
				$time = substr($eventTime,0,4).'-'.substr($eventTime,4,2).'-'.substr($eventTime,6,2).' '.substr($eventTime,9,2).':'.substr($eventTime,11,2).':'.substr($eventTime,13,2);
				$datar='';
				if($arr['service']['data']['Base_apart']==1){
					$datar = 'b18010ff0000e1'
				}
				if($arr['service']['data']['Alarm_check']==1){
					$datar = 'b18080ff0000e1';
				}
				if($arr['service']['data']['Alarm_fire']==1){
					$datar = 'b18800ff0000e1';
				}
				if($datar != ''){
				    $data = [
			            'hd'=>[
			                'ver'=>'0',
			                'rdTk'=>'1234',
			                'pktp'=>0,
			                'gwad'=>"T6CN470S0019" //
			            ],
			            'rxpk'=>[
			                'time'=>$time, //
			                'tmst'=>strtotime($time), //
			                'freq'=>472.9, //
			                'datr'=>'SF12BW125', //
			                'rssi'=>'-93.0', //
			                'lsnr'=>'17.9', //
			                'chan'=>7, //
			                'mote'=>[
			                    'DevE'=>'0000000000000391', //
			                    'DevA'=>913,
			                    'AppE'=>'BB02000000AB1111', //
			                    'fcnt'=>626, //
			                    'fprt'=>2, //
			                    'fmtp'=>2,
			                    'data'=>'+hZMSDA1MTQxMDAwMDAwMDAwMzkxEAD8',
			                    // 'datar'=>'b18001ff0000e1' //
			                    'datar'=>$datar //
			                ]
			            ],
			            'action'=>'app_data'
			        ];
					to_url('http://iot.iqykj.com/api/lora',http_build_query($data));	
				}	
	    	}
	    }
	}
	
	function to_url($url, $data='')
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }